{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# C++的调用其他类"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 类中有其他的类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "\n",
    "//点和圆关系案例\n",
    "\n",
    "//点类\n",
    "class Point\n",
    "{\n",
    "public:\n",
    "    //设置x\n",
    "    void setX(int x)\n",
    "    {\n",
    "        m_X = x;\n",
    "    }\n",
    "    //获取x\n",
    "    int getX()\n",
    "    {\n",
    "        return m_X;\n",
    "    }\n",
    "    //设置y\n",
    "    void setY(int y)\n",
    "    {\n",
    "        m_Y = y;\n",
    "    }\n",
    "    //获取y\n",
    "    int getY()\n",
    "    {\n",
    "        return m_Y;\n",
    "    }\n",
    "\n",
    "private:\n",
    "    int m_X;\n",
    "    int m_Y;\n",
    "};\n",
    "\n",
    "//圆类\n",
    "class Circle\n",
    "{\n",
    "public:\n",
    "    //设置半径\n",
    "    void setR(int r)\n",
    "    {\n",
    "        m_R = r;\n",
    "    }\n",
    "    //获取半径\n",
    "    int getR()\n",
    "    {\n",
    "        return m_R;\n",
    "    }\n",
    "    //设置圆心\n",
    "    void setCenter(Point center)\n",
    "    {\n",
    "        m_Center = center;\n",
    "    }\n",
    "    //获取圆心\n",
    "    Point getCenter()\n",
    "    {\n",
    "        return m_Center;\n",
    "    }\n",
    "private:\n",
    "    int m_R; //半径\n",
    "    Point m_Center; //圆心   //在类中可以让另一个类 作为本类中的成员\n",
    "};\n",
    "\n",
    "//判断点和圆关系\n",
    "void isInCircle(Circle& c, Point& p)\n",
    "{\n",
    "    //计算两点之间距离 平方\n",
    "    int distance =\n",
    "        (c.getCenter().getX() - p.getX()) * (c.getCenter().getX() - p.getX()) +\n",
    "        (c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY());   //c.getCenter()返回的是圆心点center类\n",
    "                                                                                 //可以调用圆心点center类中的方法\n",
    "\n",
    "    //计算半径的平方\n",
    "    int rDistance = c.getR() * c.getR();\n",
    "\n",
    "    //判断关系\n",
    "    if (distance == rDistance)\n",
    "    {\n",
    "        cout << \"点在圆上\" << endl;\n",
    "    }\n",
    "    else if (distance > rDistance)\n",
    "    {\n",
    "        cout << \"点在圆外\" << endl;\n",
    "    }\n",
    "    else\n",
    "    {\n",
    "        cout << \"点在圆内\" << endl;\n",
    "    }\n",
    "}\n",
    "\n",
    "int main()\n",
    "{\n",
    "    //创建圆\n",
    "    Circle c;\n",
    "    c.setR(10);\n",
    "    Point center;\n",
    "    center.setX(10);       //设置点的横坐标\n",
    "    center.setY(0);        //设置点的纵坐标\n",
    "    c.setCenter(center);   //设置点类传入圆类\n",
    "\n",
    "    //创建点\n",
    "    Point p;\n",
    "    p.setX(10);\n",
    "    p.setY(10);\n",
    "\n",
    "    //判断关系\n",
    "    isInCircle(c, p);\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果:  \n",
    " - 点在圆上  \n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 导入其他文件的类"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.1 point.h头文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "//这是point.h头文件\n",
    "\n",
    "#pragma once //防止头文件重复包含，防止头文件冲突\n",
    "#include <iostream> //标准输入输出\n",
    "using namespace std; //标准命名空间\n",
    "\n",
    "//只要函数声明、变量声明\n",
    "//成员函数只需要声明就好了，末尾有分号。\n",
    "class Point\n",
    "{\n",
    "public:\n",
    "    //设置x\n",
    "    void setX(int x);\n",
    "\n",
    "    //获取x\n",
    "    int getX();\n",
    "\n",
    "    //设置y\n",
    "    void setY(int y);\n",
    "\n",
    "    //获取y\n",
    "    int getY();\n",
    "\n",
    "\n",
    "private:\n",
    "    int m_X;\n",
    "    int m_Y;\n",
    "};"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.2 point.cpp源文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "//这是point.cpp源文件\n",
    "#include \"point.h\"\n",
    "\n",
    "//定义函数时，要加上作用域，\"双冒号::\"表示Point作用域下的函数\n",
    "void Point::setX(int x)\n",
    "{\n",
    "    m_X = x;\n",
    "}\n",
    "//获取x\n",
    "int Point::getX()\n",
    "{\n",
    "    return m_X;\n",
    "}\n",
    "//设置y\n",
    "void Point::setY(int y)\n",
    "{\n",
    "    m_Y = y;\n",
    "}\n",
    "//获取y\n",
    "int Point::getY()\n",
    "{\n",
    "    return m_Y;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.3 circle.h头文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#pragma once \n",
    "#include <iostream>  //标准输出流\n",
    "using namespace std; //标准命名空间\n",
    "#include \"Point.h\"  //一个类中用到另一个类，把另一个类包含的头文件包含进来\n",
    "\n",
    "//圆类\n",
    "class Circle\n",
    "{\n",
    "public:\n",
    "    //设置半径\n",
    "    void setR(int r);\n",
    "\n",
    "    //获取半径\n",
    "    int getR();\n",
    "\n",
    "    //设置圆心\n",
    "    void setCenter(Point center);\n",
    "\n",
    "    //获取圆心\n",
    "    Point getCenter();\n",
    "\n",
    "private:\n",
    "    int m_R; //半径\n",
    "    Point m_Center; //圆心   //在类中可以让另一个类 作为本类中的成员\n",
    "};"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.4 circle.cpp头文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "//这是circle.cpp头文件\n",
    "\n",
    "#include \"circle.h\"\n",
    "\n",
    "//设置半径\n",
    "void Circle::setR(int r)\n",
    "{\n",
    "    m_R = r;\n",
    "}\n",
    "\n",
    "//获取半径\n",
    "int Circle::getR()\n",
    "{\n",
    "    return m_R;\n",
    "}\n",
    "\n",
    "//设置圆心\n",
    "void Circle::setCenter(Point center)\n",
    "{\n",
    "    m_Center = center;\n",
    "}\n",
    "\n",
    "//获取圆心\n",
    "Point Circle::getCenter()\n",
    "{\n",
    "    return m_Center;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.5 main.cpp文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "//这个是主文件 .cpp文件\n",
    "\n",
    "#include <iostream>\n",
    "using namespace std;\n",
    "#include \"circle.h\" //想用点类，就要包含点类的头文件\n",
    "#include \"point.h\"  //想用圆类，就要包含点类的头文件\n",
    "\n",
    "\n",
    "//判断点和圆关系\n",
    "void isInCircle(Circle& c, Point& p)\n",
    "{\n",
    "    //计算两点之间距离 平方\n",
    "    int distance =\n",
    "        (c.getCenter().getX() - p.getX()) * (c.getCenter().getX() - p.getX()) +\n",
    "        (c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY());   //c.getCenter()返回的是圆心点center类\n",
    "                                                                                 //可以调用圆心点center类中的方法\n",
    "\n",
    "    //计算半径的平方\n",
    "    int rDistance = c.getR() * c.getR();\n",
    "\n",
    "    //判断关系\n",
    "    if (distance == rDistance)\n",
    "    {\n",
    "        cout << \"点在圆上\" << endl;\n",
    "    }\n",
    "    else if (distance > rDistance)\n",
    "    {\n",
    "        cout << \"点在圆外\" << endl;\n",
    "    }\n",
    "    else\n",
    "    {\n",
    "        cout << \"点在圆内\" << endl;\n",
    "    }\n",
    "}\n",
    "\n",
    "int main()\n",
    "{\n",
    "    //创建圆\n",
    "    Circle c;\n",
    "    c.setR(10);\n",
    "    Point center;\n",
    "    center.setX(10);       //设置点的横坐标\n",
    "    center.setY(0);        //设置点的纵坐标\n",
    "    c.setCenter(center);   //设置点类传入圆类\n",
    "\n",
    "    //创建点\n",
    "    Point p;\n",
    "    p.setX(10);\n",
    "    p.setY(10);\n",
    "    \n",
    "    //判断关系\n",
    "    isInCircle(c, p);\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
